/*******************************************************************************
 * BaseView extends
 ******************************************************************************/

gmip.BaseViewPlugin = (function(){
	
	BV = mstr.views.BaseView;
	BV.prototype.renderTravelStyle = function() {
		
		if(this.newPrompt) {
			this.inputDialogCloseButton.click(BV.onInputDialogClose);
			this.travelOperatorButton.click(BV.onOperatorButtonClick);
			this.renderTravelSelectdContainer();
			return this.newPrompt;
		}
		
		this.inputDialog = $(this.get(mstr.$W.ELEMENT));
		//console.log(inputDialog);
		//var parentContainer = this.inputDialog.parent();
		
		
		this.newPrompt = $("<div style=\"padding: 10px;  \"></div>");
	
		//("<button onclick=\"$('#" + this.get("id") + "').show()\">show</button>");
		//("<button onclick=\"$('#" + this.get("id") + "').hide()\">hide</button>");
		
		this.newPrompt.append(this._createTravelPromptQuestionView());
		
		//parentContainer.append(newPrompt);
		
		// setting input dialog
		this.inputDialog.addClass("epq_travel_inputDialog");
		this.inputDialog.hide();
		
		this.inputDialogCloseButton = $("<button>Close</button>");
		this.inputDialogCloseButton.attr("pqid", this.get("id"));
		this.inputDialogCloseButton.addClass("epq_travel_inputDialogButton");
		this.inputDialogCloseButton.click(BV.onInputDialogClose);
		
		this.inputDialog.append(this.inputDialogCloseButton);
		
		this.newPrompt.append(this.inputDialog);

		return this.newPrompt;
	}
	
	BV.prototype._createTravelPromptQuestionView = function() {
		
		var id = this.get("id");
		
		// create container
		var travelContainer = $("<div></div>");
		travelContainer.attr("id", id + "_travel_container");
		travelContainer.addClass("epq_travel_container");
		
		// create selected panel
		this.travelOperatorButton = $("<div>In List</div>");
		this.travelOperatorButton.attr("id", id + "_travel_operatorButton");
		this.travelOperatorButton.attr("pqid", id);
		this.travelOperatorButton.addClass("epq_travel_operatorButton");
		this.travelOperatorButton.click(BV.onOperatorButtonClick);
		
		travelContainer.append(this.travelOperatorButton);
		
		this.travelSelectdContainer = $("<div></div>");
		this.travelSelectdContainer.attr("id", id + "_travel_selectdContainer");
		this.travelSelectdContainer.addClass("epq_travel_selectdContainer");
		
		travelContainer.append(this.travelSelectdContainer);
		
		// console.log(travelContainer);
		
		return travelContainer;
	}
	
	BV.prototype.renderTravelSelectdContainer = function() {
		
		var selectedLM = this.get('model').get('answer').get('selected');
		
		if(!selectedLM) {
			return;
		}
			
		this.travelSelectdContainer.empty();
		
		var id = this.get("id");
		var items = selectedLM.get("items");
		for (var i = 0; i < items.length; i++) {
			
			var removeButton = $("<img style='cursor:pointer' src='../plugins/gmipPE/style/images/close_16x16.png'>");
			removeButton.attr("pqid", id);
			removeButton.attr("dssid", items[i].dssid);
			removeButton.click(BV.onRemoveElement);

			var travelElement = $("<div> " + items[i].n + "&nbsp;</div>");
			travelElement.attr("id", id + "_travel_element");
			travelElement.addClass("epq_travel_element");
			
			travelElement.append(removeButton);
			
			this.travelSelectdContainer.append(travelElement);
		}
		
		
	}
	
	BV.prototype.openInputDialog = function () {
		// console.log("Prompt Question ID: " + this.get("id"));
		
		var dialog = $("#" + this.get("id"));
		// dialog.css("position","absolute");
		dialog.css("top", ($(document).scrollTop() + ($(window).height() - dialog.height()) * 0.5) + "px");
		dialog.css("left", ($(window).width() - dialog.width()) * 0.5 + "px");
		dialog.show();
	}
	
	BV.prototype.closeInputDialog = function () {
//		LC.execRemove
		$("#" + this.get("id")).hide();
		this.renderTravelSelectdContainer();
	}
	
	
	BV.prototype.removeElementFromTravelSelectdContainer = function (dssid) {
		
		/*if(dssid.indexOf(":")!=-1) {
			dssid = dssid.substring(0, dssid.indexOf(":"));
		}*/
		
		var listCart = this.get('model').get('answer').get('listCart');
		var selectedItems = this.get('model').get('answer').get("selected").getItems();
		for ( var i = 0; i < selectedItems.length; i++) {
			if(selectedItems[i].dssid === dssid) {
				listCart.get("selected").execSingleSelect(i); 
				listCart.execRemove();
				break;
			}
		}
		
		this.renderTravelSelectdContainer();
	}
	
	// events
	BV.onOperatorButtonClick = function () {
		
		var pqid = this.attributes["pqid"] && this.attributes["pqid"].value;
		
		if (pqid) {
			var pq = mstr.$obj(pqid);
			pq && pq.openInputDialog();
		} 
	}
	
	BV.onInputDialogClose = function () {
		var pqid = this.attributes["pqid"] && this.attributes["pqid"].value;
		if (pqid) {
			var pq = mstr.$obj(pqid);
			pq && pq.closeInputDialog();
		}
	}
	
	BV.onRemoveElement = function () {
		// console.log("BV.onRemoveElement");
		var pqid = $(this).attr("pqid");
		var dssid = $(this).attr("dssid");
		if (pqid && dssid) {
			var pq = mstr.$obj(pqid);
			pq && pq.removeElementFromTravelSelectdContainer(dssid);
		}
	}
	
})();

$(function(){ 

	/*$(".promptBnt").append("<button onclick='gmip.events.BaseView_View()'>**MAGIC**</button>");
	
	gmip.events = gmip.events || {};
	gmip.events.BaseView_View = function() {
		var views = mstrView.get("views");
		//console.log(views);
		for (var i = 0; i < views.length; i++ ) {
			views[i].renderTravelStyle();
		}
	}*/
	
});